package top.greatxiaozou.config;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import top.greatxiaozou.pojo.Admin;
import top.greatxiaozou.service.AdminService;

public class AdminRealm extends AuthorizingRealm {

    @Autowired
    AdminService adminService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;

        Admin admin = adminService.selectAdminByName(token.getUsername());
        if (admin==null){
            return null;//此处抛出异常
        }else {
            return new SimpleAuthenticationInfo(admin,admin.getPassword(),"");
        }



    }
}
